package com.hishop.service.impl;

import com.google.common.collect.Lists;
import com.hishop.service.IFileService;
import com.hishop.util.FTPUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by hejie on 2017/7/18.
 */
@Service("iFileService")
@Slf4j
public class FileServiceImpl implements IFileService {

    //private Logger logger= LoggerFactory.getLogger(FileServiceImpl.class);

    @Override
    public String upload(MultipartFile file, String path) {
       String fileName=file.getOriginalFilename();
        //扩展名
        String fileExtensionName=fileName.substring(fileName.lastIndexOf("."));
        String uploadFileName= UUID.randomUUID().toString()+fileExtensionName;
        log.info("开始上传文件，上传文件的文件名:{},上传的路径:{},新文件名:{}",fileName,path,uploadFileName);
        File fileDir=new File(path);
        if (!fileDir.exists()){
            fileDir.setWritable(true);
            fileDir.mkdirs();
        }
        File targetFile=new File(path,uploadFileName);
        try {
            file.transferTo(targetFile);
            //文件已经上传成功
            //TODO: 将targetFile上传到我们的FTP服务器上
             FTPUtil.uploadFile(Lists.newArrayList(targetFile));
            // TODO:  上传完之后，删除upload下面的文件
             targetFile.delete();
        } catch (IOException e) {
            log.error("上传文件异常",e);
            return null;
        }

        return targetFile.getName();
    }
}
